<!DOCTYPE html>
<html lang="en">

<head>
  <title>Getting Started with ml5.js</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="../common/tensorflow-0.9.0.js"></script>
</head>

<body>

  <script>
    //射箭游戏需要拟合两条抛物线,所以需要4个神经元

    //第一层 参数
    var w1 = tf.tensor([-3.288,5.791,29.392,-13.817], [1, 4])  //1个参数,四个神经元
    var b1 = tf.tensor([-0.365,-1.031,-5.902,3.069], [1, 4])


    //第二层 参数
    var w2 = tf.tensor([-0.878,-0.704,-0.212,-0.517], [4, 1])  //4个参数,一个神经元
    var b2 = tf.tensor([-0.27], [1, 1])


    //正向传播
    function predict(x) {
      var input = tf.tensor(x, [1, 1])
      var l1 = input.mul(w1).add(b1).tanh()
      var l2 = l1.mul(w2).add(b2).tanh()
      return l2.dataSync()[0]

    }

    var game = JianGame(null,400,600)

    async function init(){
        while(true){
          var stateObj = game.getState()
          if(false == stateObj.stepOver){
            var output = predict(stateObj.state)
            game.step(output)
          }
        }

        await tf.nextFrame()
    }

    init()

  </script>
</body>

</html>